package foodtable;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;

import com.andro.chansung.ConstantId;
import com.andro.chansung.ConstantName;
import com.andro.chansung.ConstantURL;
import com.andro.chansung.DB;

public class DainFoodUpdateThread extends Thread {
	
	private Activity activity;
	private Handler handler;
	private boolean isADong;
	private boolean isBioTech;
	
	public DainFoodUpdateThread(Activity activity, Handler handler, boolean isADong, boolean isBioTech) {
		this.activity = activity;
		this.handler  = handler;
		this.isADong = isADong;
		this.isBioTech = isBioTech;
	}
	
	public void run() {
		try {
			Document doc = null;
			
			SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
			
			if( isADong ) {
				doc = Jsoup.connect(ConstantURL.FOOD_A_DONG_TABLE_URL).get();
			} else if( isBioTech ) {
				doc = Jsoup.connect(ConstantURL.FOOD_BIO_TECH_TABLE_URL).get();
			} else {
			    doc = Jsoup.connect(ConstantURL.FOOD_DAIN_TABLE_URL).get();
			}
			
			Elements lunches = doc.select("lunch");
			Elements dinners = doc.select("dinner");
			
			if( isADong || isBioTech ) {
				Elements breakfast = doc.select("breakfast");
				
				for( Element element : breakfast ) {
					inserData(db, element, ConstantId.FOOD_TIME.BREAKFAST, isADong, isBioTech);
				}
			}
			
			if( !isBioTech ) {
				for( Element element : lunches ) {
					inserData(db, element, ConstantId.FOOD_TIME.LUNCH, isADong, isBioTech);
				}
			}
			
			for( Element element : dinners ) {
				inserData(db, element, ConstantId.FOOD_TIME.DINNER, isADong, isBioTech);
			}
			db.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		handler.sendEmptyMessage(0);
	}
	
	public void inserData(SQLiteDatabase db, Element element, ConstantId.FOOD_TIME time, boolean isADong, boolean isBioTech) {
		DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
											  isADong, isBioTech,
											  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
													  time,
													   ConstantId.FOOD_DAY.MONDAY,
													   element.select("mon menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));

		DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
				  							  isADong, isBioTech,
											  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
													  time,
													   ConstantId.FOOD_DAY.TUESDAY,
													   element.select("tue menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));
		
		DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
											  isADong, isBioTech,
											  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
													  time,
													   ConstantId.FOOD_DAY.WEDNESDAY,
													   element.select("wed menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));
		
		DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
											  isADong, isBioTech,
											  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
													  time,
													   ConstantId.FOOD_DAY.THURSDAY,
													   element.select("thu menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));
		
		DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
											  isADong, isBioTech,
											  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
													  time,
													   ConstantId.FOOD_DAY.FRIDAY,
													   element.select("fri menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));
		
		if( isADong || isBioTech ) {
			DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
					  isADong, isBioTech,
					  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
							  time,
							   ConstantId.FOOD_DAY.SATURDAY,
							   element.select("sat menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));
		} 
		
		if( isBioTech ) {
			DB.insertDataIntoDainOrADongOrBioFoodTable(db, 
					  isADong, isBioTech,
					  new Food(ConstantId.FOOD_KIND.valueOf(element.select("kind").get(0).text()),
							  time,
							   ConstantId.FOOD_DAY.SUNDAY,
							   element.select("sun menu").get(0).text().replaceAll("'", "*").replaceAll("<br>", "\n")));		
		}
	}
}
